<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.userName"
              :disabled="detailsDisabled"
              placeholder="请输入标签名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <perButton label="新建" type="primary" icon="el-icon-plus" size="medium" perms="PT_USER_ADD"
                   @click="handlerAccount()"></perButton>
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :tree-props="{children: 'childList'}"
        row-key="id"
        border
        stripe
        style="width: 100%;"
        class="elTable"
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column prop="tagName" label="标签名称" width="300" header-align="center"/>
        <el-table-column label="级别" width="80" align="center">
          <template slot-scope="scope">
            {{ Number(scope.row.tagLevel) + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="tagDesc" show-overflow-tooltip label="备注" align="center"/>
        <el-table-column prop="createByName" label="创建人" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column prop="updateByName" label="更新人" align="center"/>
        <el-table-column label="更新时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="320" align="center">
          <template #default="{ row }">
            <perButton label="添加子标签" type="primary" size="mini" perms="PT_USER_ADD"
                       @click="addChildTab(row)"></perButton>
            <perButton label="编辑" type="warning" size="mini" perms="PT_USER_EDIT"
                       @click="handlerEdit(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PT_USER_DEL"
                       @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 弹框 -->
    <yc-dialog ref="ycDialog" :dialogTitle="dialogTitle" :centerTitle="centerTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="newAccountFormRef"
            :rules="newAccountModelRules"
            :model="newAccountModel"
            :disabled="detailsDisabled"
            label-width="90px"
          >
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="newAccountModel.tagName"/>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="newAccountModel.tagDesc" type="textarea" :rows="4"/>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addOrUpdateLoading" @click="dialogSave">确定</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <!-- 添加子标签 -->
    <yc-dialog ref="childDialog" :dialogTitle="dialogTitle" :centerTitle="centerTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="newChildFormRef"
            :rules="newChildModelRules"
            :model="newChildModel"
            :disabled="detailsDisabled"
            label-width="90px"
          >
            <el-form-item label="上级标签" prop="parentName">
              <el-input :disabled="true" v-model="newChildModel.parentName"/>
            </el-form-item>
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="newChildModel.tagName"/>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="newChildModel.tagDesc" type="textarea" :rows="4"/>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addOrUpdateLoading" @click="childDialogSave">确定</el-button>
        <el-button @click="$refs.childDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <!-- 封装操作后弹框 -->
    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"

  import {
    queryList,
    insertByModel,
    updateByModel,
    deleteByModel,
  } from "@/api/projectBasicsSetup/tagManage"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        isGlobalMessageShow: false,
        message: "",

        projectCode: "",
        tableData: [],
        loading: false,

        searchModel: {
          userName: "",
          status: null //正常/停用状态（全部）
        },

        newAccountModel: {
          tagName: "",
          tagDesc: "",
          id: 0
        },
        newAccountModelRules: {
          tagName: [{required: true, message: "请输入标签名称", trigger: "blur"}]
        },

        dialogTitle: "",
        centerTitle: "",

        newChildModel: {
          parentName: '',
          parentCode: '',
          tagName: "",
          tagDesc: "",
          id: 0
        },
        newChildModelRules: {
          tagName: [{required: true, message: "请输入标签名称", trigger: "blur"}],
        },

        detailsDisabled: false,
        addOrUpdateLoading: false
      }
    },
    created() {
      this.projectCode = sessionStorage.getItem('projectCode')
      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        if (tableInfo) {
          const {userName, status} = tableInfo
          const {object} = await queryList({
            projectCode: this.projectCode,
            userName,
            status
          })
          this.tableData = object
          this.loading = false
        } else {
          const {object} = await queryList({
            projectCode: this.projectCode,
          })
          this.tableData = object
          this.loading = false
        }
      },
      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },


      /**
       * 删除按钮
       */
      async handlerDelete({id}) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const {message, status} = await deleteByModel({id})
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },


      /**
       * 新建
       * */
      async handlerAccount(row) {
        // if(row == 'add') {
        this.dialogTitle = "添加"
        this.centerTitle = "添加"

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.ycDialog.dialogVisible = true

        Object.keys(this.newAccountModel).forEach((item) => {
          this.newAccountModel[item] = ""
        })
        // }
      },

      /**
       * 修改
       */
      handlerEdit(row) {
        if (row.tagLevel == 0) {
          this.dialogTitle = "编辑"
          this.centerTitle = "编辑"

          this.newAccountModel.id = row.id
          this.newAccountModel.tagName = row.tagName
          this.newAccountModel.tagDesc = row.tagDesc

          this.addOrUpdateLoading = false
          this.detailsDisabled = false
          this.$refs.ycDialog.dialogVisible = true
        } else {
          this.dialogTitle = "编辑子标签"
          this.centerTitle = "编辑子标签"

          this.newChildModel.id = row.id
          this.newChildModel.parentName = row.parentName
          this.newChildModel.parentCode = row.tagCode
          this.newChildModel.tagName = row.tagName
          this.newChildModel.tagDesc = row.tagDesc

          this.detailsDisabled = false
          this.addOrUpdateLoading = false
          this.$refs.childDialog.dialogVisible = true
        }
      },
      dialogSave() {
        this.$refs.newAccountFormRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true
            if (this.detailsDisabled) {
              // 详情的确定
              this.$refs.ycDialog.dialogVisible = false
            } else if (this.newAccountModel.id) {
              const {message, status} = await updateByModel({
                projectCode: this.projectCode,
                ...this.newAccountModel,
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const {tagName, tagDesc, id} = this.newAccountModel
              const {message, status} = await insertByModel({
                projectCode: this.projectCode,
                tagName, tagDesc, id
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            }
            this.addOrUpdateLoading = false
          }
        })
      },

      addChildTab(row) {
        this.dialogTitle = "添加子标签"
        this.centerTitle = "添加子标签"

        this.newChildModel.id = ''
        this.newChildModel.parentName = row.tagName
        this.newChildModel.parentCode = row.tagCode
        this.newChildModel.tagName = ''
        this.newChildModel.tagDesc = ''

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.childDialog.dialogVisible = true
      },
      childDialogSave() {
        this.$refs.newChildFormRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true
            if (this.newChildModel.id) {
              const {id, tagName, tagDesc} = this.newChildModel
              const {message, status} = await updateByModel({
                id, tagName, tagDesc
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.childDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const {tagName, tagDesc, parentCode, parentName} = this.newChildModel
              const {message, status} = await insertByModel({
                projectCode: this.projectCode,
                tagName, tagDesc, parentCode, parentName
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.childDialog.dialogVisible = false
                this.$message.success(message)
              }
            }

            this.addOrUpdateLoading = false
          }
        })
      },
      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          userName: "",
          status: null
        }
        this.getAccountData()
      },

      /**
       * globalMessage的知道按钮的方法
       * */
      receive() {
        this.isGlobalMessageShow = false
      },
    }
  }
</script>

<style lang="scss" scoped>
  .status {
    display: inline-block;
    padding: 0 10px;
  }

  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
  }



  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    // justify-content: st-end;
    padding: 5px 20px;
  }
</style>
